package cn.itcast.tags.meta

import cn.itcast.tags.utils.DateUtils

case class HBaseMeta(
                      zkHosts: String,
                      zkPort: String,
                      hbaseTable: String,
                      family: String,
                      selectFieldNames: String,
                      filterConditions: String
                    )

object HBaseMeta {
  def getHBaseMeta(ruleMap: Map[String, String]): HBaseMeta = {

    val whereCondition: String = ruleMap.getOrElse("whereCondition", null)

    val conditionStr: String = if(null!=whereCondition){
      val Array(field,unit,amount)=whereCondition.split("#")
      val nowDate: String = DateUtils.getNow(DateUtils.SHORT_DATE_FORMAT)
      val yesterdayDate: String = DateUtils.dateCalculate(nowDate, -1)
      val agoDate: String = unit match {
        case "day" => DateUtils.dateCalculate(yesterdayDate, -amount.toInt)
        case "month" => DateUtils.dateCalculate(yesterdayDate, -amount.toInt * 30)
        case "year" => DateUtils.dateCalculate(yesterdayDate, -amount.toInt * 365)
      }

      s"$field[GE]$agoDate,$field[LE]$yesterdayDate"
    }else null
    HBaseMeta(
      ruleMap("zkHosts"),
      ruleMap("zkPort"),
      ruleMap("hbaseTable"),
      ruleMap("family"),
      ruleMap("selectFieldNames"),
      conditionStr
    )
  }
}
